package com.yoogor.xposedtest;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * @author caipengrui 2018/6/11
 * Package com.yoogor.xposedtest
 * Title:  JsoupParse
 * Description: (用一句话描述该文件做什么)
 * Copyright (c) 2018 优菓科技有限公司 All rights reserved.
 * Create DateTime: 2018/6/11.
 */
public class JsoupParse {
    public static List<MMMessageEntity> parse(String html) {
        List<MMMessageEntity> list = new ArrayList<>();

        Document doc = Jsoup.parse(html);
        Elements media = doc.select("div.weui_media_box");
        for (Element element : media) {
            MMMessageEntity mmMessageEntity = new MMMessageEntity();
            mmMessageEntity.setContentHtml(element.html());
            mmMessageEntity.setUrl(element.attr("hrefs"));
            mmMessageEntity.setDataType(element.selectFirst("div.weui_media_bd").attr("data-type"));
            if (!element.select(".weui_media_title").isEmpty()) {
                mmMessageEntity.setTitle(element.selectFirst(".weui_media_title").text());
            }
            if (!element.select(".weui_media_desc").isEmpty()) {
                mmMessageEntity.setDesc(element.selectFirst(".weui_media_desc").text());
            }
            if (!element.select(".weui_media_extra_info").isEmpty()) {
                mmMessageEntity.setExtraInfo(element.selectFirst(".weui_media_extra_info").text());
            }
            if (!element.select("span.weui_media_hd").isEmpty()) {
                mmMessageEntity.setShortcat(element.selectFirst("span.weui_media_hd").attr("style"));
            }
            list.add(mmMessageEntity);
        }

        return list;
    }
}
